package com.cqie.tes.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqie.tes.comm.R;
import com.cqie.tes.comm.qo.BaseQo;
import com.cqie.tes.comm.service.impl.BaseServiceImpl;
import com.cqie.tes.domain.Courseclazzteacher;
import com.cqie.tes.domain.qo.tes.CourseclazzteacherQo;
import com.cqie.tes.domain.vo.tes.courseclazzteacher.CourseclazzteacherVo;
import com.cqie.tes.mapper.CourseclazzteacherMapper;
import com.cqie.tes.service.ClazzService;
import com.cqie.tes.service.CourseService;
import com.cqie.tes.service.CourseclazzteacherService;
import com.cqie.tes.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fall in love
 * @since 2021-10-15
 */
@Service
public class CourseclazzteacherServiceImpl extends BaseServiceImpl<CourseclazzteacherMapper, Courseclazzteacher> implements CourseclazzteacherService {

    @Autowired
    private CourseclazzteacherMapper courseclazzteacherMapper;
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private EmployeeService employeeService;
    @Override
    public R getList(BaseQo query) {
        // 1
        CourseclazzteacherQo qo = (CourseclazzteacherQo) query;
//        System.out.println(query.getLimit());
        // 2
        QueryWrapper<Courseclazzteacher> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(qo.getName())){
            qw.like("name",qo.getName());
        }
        qw.eq("mark", 1);
        qw.orderByDesc("id");
        // 3
        IPage<Courseclazzteacher> page = new Page<>(qo.getPage(),qo.getLimit());
        IPage<Courseclazzteacher> data = courseclazzteacherMapper.selectPage(page,qw);
        List<Courseclazzteacher> entityList = data.getRecords();

        // 4
        List<CourseclazzteacherVo> voList = new ArrayList<>();

        if (!entityList.isEmpty()) {
            entityList.forEach(item->{
                CourseclazzteacherVo vo = new CourseclazzteacherVo();
                BeanUtils.copyProperties(item,vo);
                // 设置vo字段
                if (vo.getClazzId() != null){
                    vo.setClazzName(clazzService.getById(vo.getClazzId()).getName());
                }
                if (vo.getCourseId() != null){
                    vo.setCourseName(courseService.getById(vo.getCourseId()).getName());
                }
                if (vo.getEmpId() != null){
                    vo.setEmpName(employeeService.getById(vo.getEmpId()).getName());
                }
                //
                voList.add(vo);
            });
        }
        Long total = data.getTotal();
//        System.out.println(total);

        return R.ok().message("查询成功").data("total", total).data("rows", voList);
    }


    @Override
    public R edit(Courseclazzteacher entity) {
        return super.edit(entity);
    }

    @Override
    public R deleteById(Integer id) {
        if (id == null || id == 0) {
            return R.error().message("记录ID不能为空");
        }
        Courseclazzteacher entity = this.getById(id);
        if (entity == null) {
            return R.error().message("记录不存在");
        }
        return super.delete(entity);
    }
}
